var Admin = require('../handlers/adminHandler.js')
var User = require('../handlers/userHandler')
var Apply = require('../handlers/applyManageHandler')
var Active = require('../handlers/activeHandler')
var config = require('../config/config.js')

module.exports = {

  //  后台登陆
  login: function(req, res) {
    var query = req.query
    var adminUser = query.user
    var password = query.password

    if (adminUser == config.admin.user && password == config.admin.password) {
      req.session.adminId = config.admin.adminId
      return res.json({
        code: 100,
        msg: '登录成功'
      })
    } else {
      User.findByName(adminUser, function(err, user) {
        if (err) {
          console.log('adminController login fail. err = ' + err)
          return res.json({
            code: 177,
            msg: '验证失败'
          })
        }
        if (user && user.identity > 0) {
          if (user.name == adminUser && user.password == password) {
            req.session.adminId = user.id
            return res.json({
              code: 100,
              msg: '登录成功'
            })
          } else {
            return res.json({
              code: 122,
              msg: '用户名或密码错误'
            })
          }
        } else {
          return res.json({
            code: 122,
            msg: '无权登陆'
          })
        }

      })

    }
  },
  getAll: function(req, res) {

    if (!(req.session.adminId == config.admin.adminId)) {
      return res.json({
        code: 122,
        msg: '无权访问'
      })
    }

    User.find({}, function(err, users) {
      if (err) {
        console.log('adminController getAll fail. err = ' + err)
        return res.json({
          code: 177,
          msg: '查找失败'
        })
      }

      var admins = users.filter(function(user) {
        return user.identity > 0
      })

      return res.json({
        code: 100,
        msg: '查找成功',
        data: {
          list: admins
        }
      })
    })
  },
  getApply: function(req, res) {

    if (!(req.session.adminId == config.admin.adminId)) {
      return res.json({
        code: 122,
        msg: '无权访问'
      })
    }

    Apply.find(req.query, function(err, applys) {
      if (err) {
        console.log('adminController getApply fail. err = ' + err)
        return res.json({
          code: 177,
          msg: '获取申请列表失败'
        })
      }
      if (!applys.length) {
        return res.json({
          code: 112,
          msg: '没有申请记录'
        })
      }
      return res.json({
        code: 100,
        data: {
          list: applys
        }
      })
    })
  },
  updateApply: function(req, res) {
    var id = req.params.id
    var access = req.body.access

    Apply.findByIdAndUpdate(id, access, function(err, apply) {
      if (err) {
        console.log('adminController updateApply fail. err = ' + err)
        return res.json({
          code: 177,
          msg: err
        })
      }
      if (access) {
        updateUserIdentity(apply.handler.id, access, res)

      } else {
        return res.json({
          code: 100,
          msg: '更新成功'
        })
      }
    })

  },
  removeUser: function(req, res) {
    var id = req.params.id

    User.remove({
      id: id
    }, function(err) {
      if (err) {
        console.log('adminController removeUser fail. err = ' + err)
        return res.json({
          code: 177,
          msg: err
        })
      }
      return res.json({
        code: 100,
        msg: '删除成功'
      })
    })

  },


  //  接口渲染
  adminHomePage: function(req, res) {
    res.render('admin/index')
  },
  adminTestPage: function(req, res) {
    res.render('admin/testAdmin')
  },
  adminUserPage: function(req, res) {
    res.render('admin/userAdmin')
  },
  adminPostPage: function(req, res) {
    res.render('admin/postAdmin')
  },
  adminSitePage: function(req, res) {
    res.render('admin/siteAdmin')
  },
  adminLoginPage: function(req, res) {
    res.render('admin/login')
  }


}

function updateUserIdentity(userId, identity, res) {
  User.findByIdAndUpdate(userId, {
    handlerType: 'update',
    content: {
      identity: identity
    }
  }, function(err, user) {
    if (err) {
      console.log('adminController updateUserIdentity fail. err = ' + err)
      return res.json({
        code: 177,
        msg: err
      })
    }
    //  添加动态
    var content = '被撤销了管理员职位'
    if (identity == 1) {
      content = '被任命为管理'
    }
    var adminActive = util.createActiveType5({
      name: user.name,
      id: user.id
    }, content)

    Active.insert(adminActive)

    return res.json({
      code: 100,
      msg: '更新成功'
    })
  })
}